﻿/*
Copyright (c) 2010 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Globalization;
using System.Threading;

namespace TeachMe.Application.Forms.Options
{
    using TeachMe.Application.ComboItems;

    public partial class General : OptionsControl
    {
        public General()
        {
            InitializeComponent();
        }

        internal override void LoadOptions()
        {
            cbAutoStart.Checked = Root.AutoStart;

            // languages

            ComboBoxItem<CultureInfo> selection = null;

            foreach (string supportedCulture in Root.SupportedCultures)
            {
                CultureInfo cultureInfo;

                try
                {
                    cultureInfo = CultureInfo.GetCultureInfo(supportedCulture);
                }
                catch (Exception e)
                {
                    Logging.Error(GetType(), e, "Cannot get culture info for '{0}'.", supportedCulture);

                    continue;
                }

                ComboBoxItem<CultureInfo> item = new ComboBoxItem<CultureInfo>(cultureInfo, Utilities.ConvertToPascalCase(cultureInfo.NativeName));

                if (cultureInfo.Equals(Thread.CurrentThread.CurrentUICulture))
                {
                    selection = item;
                }

                cbLanguages.Items.Add(item);
            }

            cbLanguages.SelectedItem = selection;

            //

            cbNewVersionCheckInterval.FillIntervals();
            cbNewVersionCheckInterval.Interval = Root.UserSettings.NewVersionCheckInterval.Value;            
        }

        internal override void SaveOptions()
        {
            if (cbAutoStart.Checked != Root.AutoStart)
            {
                Root.AutoStart = cbAutoStart.Checked;
            }

            if (cbNewVersionCheckInterval.Interval != Root.UserSettings.NewVersionCheckInterval.Value)
            {
                Root.UserSettings.NewVersionCheckInterval.Value = cbNewVersionCheckInterval.Interval;
            }

            // languages

            if (cbLanguages.SelectedItem != null)
            {
                ComboBoxItem<CultureInfo> value = cbLanguages.SelectedItem as ComboBoxItem<CultureInfo>;
                if (value == null)
                {
                    throw new InvalidOperationException();
                }

                Root.UserSettings.Language.Value = value.Item.Name;
            }
        }
    }
}